using System;
using Microsoft.SharePoint;

namespace SharePoint.DataMapper
{
	[FieldMapper(typeof(SPFieldUrl), SPFieldType.URL)]
	public class FieldUrlMapper : FieldMapper
	{
		protected override SPField AddFieldInternal(ListDataContext context, SPFieldCollection collection, Type propertyType, FieldAttribute attribute)
		{
			SPFieldUrl field = (SPFieldUrl)base.AddFieldInternal(context, collection, propertyType, attribute);
			field.DisplayFormat = attribute.UrlFormat;
			return field;
		}

		protected override FieldAttribute GetFieldAttributeInternal(SPField field)
		{
			FieldAttribute attribute = base.GetFieldAttributeInternal(field);

			SPFieldUrl fieldUrl = field as SPFieldUrl;
			if (fieldUrl == null)
			{
				return attribute;
			}
			attribute.UrlFormat = fieldUrl.DisplayFormat;
			return attribute;
		}

		protected override object GetFieldInternal(ListDataContext context, object value, Type propertyType)
		{
			SPFieldUrlValue urlValue = (value is SPFieldUrlValue) ? (SPFieldUrlValue)value : new SPFieldUrlValue(value.ToString());
			return urlValue;
		}
	}
}